package cn.dlc.com.config;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author deng
 * @date 2021/6/23 11:14
 * 这是一种异常的实现方式
 **/
//@Component
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {

    // 如果有异常被补货到就会走这边了，
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {

        ModelAndView modelAndView = new ModelAndView();



        // 添加异常信息
        modelAndView.addObject("errorMsg",e.getMessage());
        if(e instanceof AccessDeniedException) {

            e.printStackTrace();

            modelAndView.setViewName("redirect:/403.jsp");
        } else {
            System.out.println("500");
            e.printStackTrace();

            modelAndView.setViewName("redirect:/500.jsp");
        }

        return modelAndView;
    }
}
